//
//  ScienceTSubViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "ScienceTSubViewController.h"
#import "ScienceTSubCollectionViewCell.h"
#define kImagePlaceholder @"article_html_content_loading.png"
@interface ScienceTSubViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIWebViewDelegate,UMSocialUIDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ScienceTSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//设置横向滑动
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.bounces = YES;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    self.view.backgroundColor = [UIColor whiteColor];
    
     self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,kP6Height(60), kWidth, kP6Height(550))];
    [self.collectionView addSubview:_webView];
    //设置 webView 背景颜色
//    [self.webView setOpaque:NO];//设置 webView 为不透明
//    self.webView.backgroundColor = [UIColor grayColor];
    self.webView.delegate = self;
    if (self.full_url.length < 500) {
    
        [self jiexiWeburl:self.weburl];
  }
   [self jiexiScienceTSub:_full_url];//解析网络

    UIImageView *commentView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kP6Height(620), kWidth, kP6Height(47))];
    commentView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:commentView];
    commentView.userInteractionEnabled = YES;
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 0.2)];
    lineView.backgroundColor = [UIColor grayColor];
    [commentView addSubview:lineView];
    
    UIButton *leftBut = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBut.frame = CGRectMake(kP6Height(20), kP6Height(5), kP6width(35), kP6Height(37));
    [leftBut setBackgroundImage:[UIImage imageNamed:@"common_icon_return@3x"] forState:UIControlStateNormal];
    [leftBut addTarget:self action:@selector(leftButAction) forControlEvents:UIControlEventTouchUpInside];
    [commentView addSubview:leftBut];
    
    UIButton *rightBut = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBut.frame = CGRectMake(kP6width(375-77), kP6Height(5), kP6width(35), kP6Height(37));
    [rightBut setBackgroundImage:[UIImage imageNamed:@"common_icon_share@3x"] forState:UIControlStateNormal];
    [rightBut addTarget:self action:@selector(rightButAction) forControlEvents:UIControlEventTouchUpInside];
    [commentView addSubview:rightBut];
    
    
}

- (void)jiexiScienceTSub:(NSString *)string
{
    NSMutableArray *imageArr = [NSMutableArray array];
    [ACHNetWork GET:string success:^(id response) {
        
        NSDictionary *minDic = [response objectForKey:@"data"];
        NSArray *arr = minDic[@"media"];
        for (NSDictionary *dic in arr) {
            NSString *imageUrl = dic[@"url"];
            [imageArr addObject:imageUrl];
        }
        NSString *strings = [minDic objectForKey:@"content"];
        NSString *op = @"http://www.myzaker.com/?_zkcmd=open_media&index=0";
        strings = [strings stringByReplacingOccurrencesOfString:op withString:@"www.baidu.com"];//替换网址,
        int i = 0;
        while ([strings containsString:kImagePlaceholder]) {
            NSRange range = [strings rangeOfString:kImagePlaceholder];
            strings = [strings stringByReplacingCharactersInRange:range withString:imageArr[i]];//替换图片
            i++;
            if (i>=imageArr.count) {
                break;
            }
        }
        [_webView loadHTMLString:strings baseURL:nil];
     
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

//网络路径展示
- (void)jiexiWeburl:(NSString *)string
{
    NSURL* url = [NSURL URLWithString:string];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
    [self.webView loadRequest:request];//加载

}


#pragma mark - 实现 collectionView 协议方法
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;//行间距
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidth, kHeight);//布局
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;//个数
}

//返回 item 内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    [collectionView registerClass:[ScienceTSubCollectionViewCell class] forCellWithReuseIdentifier:str];
    ScienceTSubCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    cell.headTitleLabel.text = self.headTitle;
    cell.autherLabel.text = self.auther_name;

    return cell;
}

#pragma mark - 点击 button
- (void)leftButAction
{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButAction
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55a87dbd67e58eb9b30039ad"
                                      shareText:self.weburl
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToQQ,UMShareToQzone,UMShareToWechatTimeline,nil]delegate:self];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //字体大小
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'"];
    //字体颜色
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
